import React from 'react';   
import {connect} from 'dva';
import {Table, Row, Col, Form, Input, Radio, Button} from 'antd';
const R = require('ramda');
import RightContacts from "../../../utils/RightConstants";
import EditProduct from '../../../components/supplier/components/EditProduct';
import RightBox from "../../../components/commons/RightBox";
const FormItem = Form.Item;
const RadioGroup = Radio.Group;

export default connect(({productApproval}) => ({productApproval}))(
    Form.create()(class productApproval extends React.Component {
        constructor(props) {
            super(props);
        }


        componentDidMount() {
            this.props.dispatch({
                type: 'productApproval/getProductApprovalList',
            });
        }

        handleResetFields = () => {
            if(this.editProduct)this.editProduct.handleResetFields();
        }



        onEdit = (record, type, isView) => {
            this.props.dispatch({type: 'productApproval/getProductById', payload: {id: record.pid}});
            this.editProduct.handleOpen(type, isView);
        }


        handleSearch = (e, flag) => {
            let values = this.props.form.getFieldsValue();
            let newPage = this.props.productApproval.productPage;
            newPage.page = flag ? newPage.page : 1;
            this.props.dispatch({
                type: 'productApproval/getProductApprovalList',
                payload: {
                    productName: values.productName,
                    status: values.status,
                    page: newPage
                }
            });
        };



        columns = [
            {title: '品名', dataIndex: 'pname', key: 'pname'},
            {title: '品牌类型', dataIndex: 'branchtypename',key: 'branchtypename'},
            {title: '品牌', dataIndex: 'branch', key: 'branch'},
            {title: '型号', dataIndex: 'model', key: 'model'},
            {title: '价格', dataIndex: 'price', key: 'price'},
            {title: '净重(kg)', dataIndex: 'weight', key: 'weight'},
            {title: '提交时间', dataIndex: 'updatetime', key: 'updatetime'},
            {title: '退税率(%)', dataIndex: 'taxrate', key: 'taxrate'},
            {title: '申报要素', dataIndex: 'declarelements', key: 'declarelements'},
            {title: '业务经理', dataIndex: 'managerName', key: 'managerName'},
            {title: '操作', key: 'key_oprator',
                render: (text, record, index) => {


                    return (
                        <span>
                        {this.props.disabled == true  || record.pstatus == 3 || record.pstatus == 4 || record.pstatus == 5 ?
                            <span>
                                 <a href="#" onClick={this.onEdit.bind(this, record, "approval", true)}>查看</a>
                            </span>
                            :
                            <span>
                                {record.pstatus == 2  ?
                                    <span>
                                        <RightBox resource={RightContacts.APPROVAL.APPROVAL_PRODUCT}><a href="#" disabled={this.props.disabled}
                                           onClick={this.onEdit.bind(this, record, "approval", false)}>审核  </a></RightBox>
                                    </span>
                                    : ""}

                            </span>
                        }
                    </span>
                    );
                }
            }];

        render() {

            const { getFieldDecorator } = this.props.form;
            let formItemLayout = {
                labelCol: { xs: { span: 24 }, sm: { span: 6 } },
                wrapperCol: { xs: { span: 24 }, sm: { span: 14 } }
            };
            let values = this.props.form.getFieldsValue();
            let pagination = {
                showTotal: total => `共 ${total} 条数据`,
                pageSize: this.props.productApproval.productPage.size,
                current: this.props.productApproval.productPage.page,
                total: this.props.productApproval.productPage.totalRow,
                showSizeChanger: true,
                onShowSizeChange: (current, size) => {
                    let newPage = this.props.productApproval.productPage;
                    newPage.size = size;
                    newPage.page = 1;
                    this.props.dispatch({
                        type: 'productApproval/getProductApprovalList',
                        payload: {
                            productName: values.productName,
                            status: values.status,
                            page: newPage
                        }
                    });
                },
                onChange: (pageNumber, pageSize) => {
                    let newPage = this.props.productApproval.productPage;
                    newPage.page = pageNumber;
                    this.props.dispatch({
                        type: 'productApproval/getProductApprovalList',
                        payload: {
                            productName: values.productName,
                            status: values.status,
                            page: newPage
                        }
                    });
                }

            }
            return (
                <div>
                    <Form>
                        <Row>
                            <Col span={7}><FormItem {...formItemLayout} label={'名称'}>{getFieldDecorator('productName')(<Input/>)}</FormItem></Col>
                            <Col span={3} style={{float: 'left'}}>
                                <Button type="primary" icon="search" style={{marginRight: 5}} onClick={this.handleSearch.bind(this)}>查询</Button>
                            </Col>

                        </Row>
                        <Row>
                            <Col span={7}>
                                <FormItem {...formItemLayout} label={'状态'}>
                                    {getFieldDecorator('status', {
                                        initialValue: "2"
                                    })(
                                        <RadioGroup style={{width: 800}}>
                                            <Radio value="2">待审核</Radio>
                                            <Radio value="3">待补充资料</Radio>
                                            <Radio value="4">审核通过</Radio>
                                            <Radio value="5">审核不通过</Radio>
                                        </RadioGroup>
                                    )}
                                </FormItem>
                            </Col>

                        </Row>
                    </Form>
                    <Table rowKey={record => record.pid} scroll={{ x: true }} dataSource={this.props.productApproval.productList} columns={this.columns} bordered={true} size={'middle'} pagination={pagination}/>

                    <EditProduct wrappedComponentRef={(inst) => this.editProduct = inst} {...this.props.productApproval}
                                 dispatch={this.props.dispatch}
                                 disabled={true}
                                 hscode ={this.props.productApproval.hscode}
                                 dataSource={this.props.productApproval.product}
                                 attachments={this.props.productApproval.attachments}
                                 handleSearch = {() => this.handleSearch(null, true)}
                    />

                </div>
            )
        }
    })


)
